/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.cn.xiaonuo.tcrfinance.tcrshops.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.common.utils.StringUtils;
import com.cn.xiaonuo.core.exception.ServiceException;
import com.cn.xiaonuo.core.pojo.page.PageResult;
import com.cn.xiaonuo.tcrfinance.tcrshops.entity.PvProductGroup;
import com.cn.xiaonuo.tcrfinance.tcrshops.enums.ProductGroupExceptionEnum;
import com.cn.xiaonuo.tcrfinance.tcrshops.mapper.PvProductGroupMapper;
import com.cn.xiaonuo.tcrfinance.tcrshops.param.PvProductGroupParam;
import com.cn.xiaonuo.tcrfinance.tcrshops.service.ProductGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class ProductGroupServiceImpl extends ServiceImpl<PvProductGroupMapper, PvProductGroup> implements ProductGroupService {

	@Autowired
	private PvProductGroupMapper pvProductGroupMapper;


	@Override
	public BusinessMessage list(PvProductGroupParam productTypeParam) {
		BusinessMessage<Object> message = new BusinessMessage<>();
		LambdaQueryWrapper<PvProductGroup> queryWrapper=new LambdaQueryWrapper<>();
		if(StringUtils.isNotEmpty(productTypeParam.getName())){
			queryWrapper.eq(PvProductGroup::getName,productTypeParam.getName());
		}

		queryWrapper.eq(PvProductGroup::getDel,0);
		List<PvProductGroup> select = pvProductGroupMapper.selectList(queryWrapper);
		message.setSuccess(true);
		message.setCode("200");
		message.setData(select);
		return message;
	}

	@Override
	public PageResult<PvProductGroup>   page( PvProductGroupParam productTypeParam){
		QueryWrapper<PvProductGroup> queryWrapper = new QueryWrapper<>();
		if (ObjectUtil.isNotNull(productTypeParam)) {

			// 根据名称 模糊查询
			if (ObjectUtil.isNotEmpty(productTypeParam.getName())) {
				queryWrapper.lambda().eq(PvProductGroup::getName, productTypeParam.getName());
			}
		}
		queryWrapper.lambda().orderByDesc(PvProductGroup::getCreatedAt);
		return new PageResult<PvProductGroup>(this.page(new Page<>(productTypeParam.getPageNo(),productTypeParam.getPageSize()), queryWrapper));
	}

	@Override
	public void add(PvProductGroupParam productTypeParam) {
		PvProductGroup productType = new PvProductGroup();
		BeanUtil.copyProperties(productTypeParam, productType);
		this.save(productType);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void delete(PvProductGroupParam productTypeParam) {
		this.removeById(productTypeParam.getId());
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void edit(PvProductGroupParam productTypeParam) {
		PvProductGroup productType = this.queryProductGroup(productTypeParam);
		BeanUtil.copyProperties(productTypeParam, productType);
		this.updateById(productType);
	}

	@Override
	public PvProductGroup detail(PvProductGroupParam productTypeParam) {
		return this.queryProductGroup(productTypeParam);
	}

	/**
	 * 获取商品类别
	 *
	 * @author Monetary
	 * @date 2021-03-05 18:16:12
	 */
	private PvProductGroup queryProductGroup(PvProductGroupParam productTypeParam) {
		PvProductGroup productType = this.getById(productTypeParam.getId());
		if (ObjectUtil.isNull(productType)) {
			throw new ServiceException(ProductGroupExceptionEnum.NOT_EXIST);
		}
		return productType;
	}
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void updateStatus(PvProductGroupParam productTypeParam) {
		PvProductGroup productType=this.detail(productTypeParam);
		productType.setDel(productTypeParam.getDel());
		this.updateById(productType);
	}

}
